import json
import requests
from requests.adapters import HTTPAdapter

import  utilss
import config

header={
    "User-Agent":"Mozilla/5.0 (iPad; CPU OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.4",
    "Referer":"https://servicewechat.com/wx8227f55dc4490f45/77/page-frame.html",
    "Content-Type":"application/json"

}

# 重复检测接口
check_url = "https://we.cqupt.edu.cn/api/mrdk/get_mrdk_flag.php"
# 打卡接口
clockin_url = "https://we.cqupt.edu.cn/api/mrdk/post_mrdk_info.php"
s= requests.Session()
s.mount('https://', HTTPAdapter(max_retries=3))


# 打卡
def clockin(clockin_url):
    utilss.get_location_info()
    config.clockin_data["timestamp"]=utilss.gettimestamp()
    config.clockin_data["mrdkkey"]=utilss.mrdk_key(utilss.now.tm_mday,utilss.now.tm_hour)
    #print(config.clockin_data)
    base64data=str({"key":utilss.tobase64(config.clockin_data)}).replace("\\n","")
    r=requests.post(clockin_url,data=base64data,headers=header)
    result_text=r.json()

    try:
        if(result_text['data']['status']==200):
            print("------------------打卡成功（发送通知）----------------------")
            utilss.send_notification(config.myinfo["push"])
        else:
            print("------------------打卡失败（发送通知）----------------------")
            utilss.send_notification(config.myinfo["push"])
    except:
        print("-----------------------打卡失败（发送通知）------------------------------")
        utilss.send_notification(config.myinfo["push"])
    return


# 检测重复
def check(check_url):
    print("---------------------重复打卡检测------------------")
    config.check_data["timestamp"]=str(utilss.gettimestamp())
    basedata=str({"key":utilss.tobase64(config.check_data)}).replace("\\n","")
    r=s.post(url=check_url, json=basedata,headers=header)

    result=r.json()
    if(result['status']==200):
        if(result['data']['count']==0):
            print("---------------打卡代码--------------------------")
            clockin(clockin_url)

        else:
            print("请勿重复打卡！！！")
    else:
        print("打卡请求错误!!! 原因："+result["message"])


    return


if __name__ == '__main__':
    # html=requests.get(check_url)
    # print(html)
    check(check_url)
    #clockin(check_url)


